#include "zd_sec_mgr.h"
#include "mfg_Stack.h"
#include "mfg.h"
#include "string.h"
#ifdef BDB_REPORTING
    #include "bdb_reporting.h"
#endif

#include "zstackapi.h"

/* macro */

/* variable */
mfgMsgHdr_t *mfgMsgHdrHead = NULL;
uint8 mfgTaskId = 0xFF;

#ifdef BDB_REPORTING
void MFG_SendReportCallback( uint8 srcEP, afAddrType_t *dstAddr, uint16 clusterID,
                            zclReportCmd_t *reportCmd, uint8 direction, uint8 disableDefaultRsp,
                            uint16 manuCode, uint8 seqNum );

#endif

/* function */

/*
* @fn          MFG_StackInit
*
* @brief       
*
* @param       destTaskId - task id
*
* @return      none
*
* */
void MFG_StackInit(uint8 destTaskId)
{
  mfgTaskId = destTaskId;
  MFG_InitCallback();
}

/*
* @fn          MFG_TaskId
*
* @brief       
*
* @param       none
*
* @return      destTaskId - task id
*
* */
uint8_t MFG_TaskId(void)
{
  return mfgTaskId;
}
/*
* @fn          MFG_CreatMsg
*
* @brief       creat mfg message
*
* @param       type - message type
*              size - message data size
*
* @return      message pointer
*
* */
mfgMsgHdr_t* MFG_CreatMsg(mfgMsgType_t type, uint8_t size)
{
  mfgMsgHdr_t* item = MFG_malloc(sizeof(mfgMsgHdr_t)+size);
  if(item)
  {
    item->type = type;
    item->size = size;
    item->next = NULL;
  }
  return item;
}

/*
* @fn          MFG_SendMsg
*
* @brief       send message to mfg task queue
*
* @param       pMsg - message pointer
*
* @return      none
*
* */
void MFG_SendMsg(mfgMsgHdr_t* pMsg)
{
  if( (pMsg != NULL) && (mfgTaskId != 0xFF) )
  {
    if(mfgMsgHdrHead == NULL)
    {
      mfgMsgHdrHead = pMsg;
    }
    else
    {
      mfgMsgHdr_t* tail = mfgMsgHdrHead;
      while(tail->next != NULL)
      {
        tail = tail->next;
      }
      tail->next = pMsg;
    }
    MFG_setEvent(mfgTaskId, MFG_MSG_IN_EVENT);
  }
}

/*
* @fn          MFG_GetMsg
*
* @brief       get message in mfg task
*
* @param       none
*
* @return      message pointer
*
* */
mfgMsgHdr_t* MFG_GetMsg(void)
{
  mfgMsgHdr_t* msg = mfgMsgHdrHead;
  if(mfgMsgHdrHead != NULL)
  {
    mfgMsgHdrHead = mfgMsgHdrHead->next;
  }
  return msg;
}

void MFG_SetupZstackCallbacks(void)
{
  zstack_devZDOCBReq_t zdoCBReq = {0};
  
  zdoCBReq.has_devStateChange = true;
  zdoCBReq.devStateChange = true;
  
  zdoCBReq.has_tcDeviceInd =true;
  zdoCBReq.tcDeviceInd = true;
  
  zdoCBReq.has_devPermitJoinInd = true;
  zdoCBReq.devPermitJoinInd = true;
  
  zdoCBReq.has_deviceAnnounce = true;
  zdoCBReq.deviceAnnounce = true;
  
  zdoCBReq.has_activeEndpointRsp = true;
  zdoCBReq.activeEndpointRsp = true;
  
  zdoCBReq.has_simpleDescRsp = true;
  zdoCBReq.simpleDescRsp = true;
  
  zdoCBReq.has_bindRsp = true;
  zdoCBReq.bindRsp = true;
  
  zdoCBReq.has_unbindRsp = true;
  zdoCBReq.unbindRsp = true;
  
  zdoCBReq.has_mgmtLeaveRsp = true;
  zdoCBReq.mgmtLeaveRsp = true;
  
  zdoCBReq.has_mgmtLqiRsp = true;
  zdoCBReq.mgmtLqiRsp = true;
  
  (void)Zstackapi_DevZDOCBReq(mfgTaskId, &zdoCBReq);
}

#ifdef BDB_REPORTING
void MFG_SendReportCallback( uint8 srcEP, afAddrType_t *dstAddr, uint16 clusterID,
                            zclReportCmd_t *reportCmd, uint8 direction, uint8 disableDefaultRsp,
                            uint16 manuCode, uint8 seqNum )
{

}
#endif


